// 测试 控制器模块
let { insertMany, count, getList, findOne, findAll, updateOne, delOne, addOne } = require('../model/goods');

// 批量写入
let insertManyCofn = async (req, res) => {
  // 读取 db/goods.json
  let dataStr = require('fs').readFileSync('./db/goods.json', 'utf-8');
  let dataArr = JSON.parse(dataStr, (k, v) => {
    if (k == 'sale_type') return v.slice(0, - 2) - 0
    if (k != 'is_select' && k != 'cart_number') return v;
  })
  let r = await insertMany(dataArr);
  if (r.status === 0) return res.send({ code: 0, message: '批量写入失败', info: r.message });
  res.send({ code: 1, message: '批量写入成功' })
}

// 商品列表控制器
let listCofn = async (req, res) => {
  let { current, pagesize, search, filter, saleType, sortType, sortMethod, category } = req.query;
  // 设置默认值
  current = current ?? 1; // 默认第一页
  pagesize = pagesize ?? 12; // 默认一页12条

  sortType = sortType ?? 'goods_id';
  if (sortType == 'id') sortType = 'goods_id';
  if (sortType == 'sale') sortType = 'sale_type';
  if (sortType == 'price') sortType = 'current_price';
  sortMethod = sortMethod ?? 'ASC';

  // 排序条件
  let sort = {};
  sort[sortType] = sortMethod == 'ASC' ? 1 : -1

  // 模糊查询正则
  let searReg = new RegExp(search, 'i');
  // 查询条件--->组装
  let cond = {};
  if (search) cond.title = searReg;
  if (category) cond.category = category;
  if (filter) {
    filter == 'hot' ? cond.is_hot = 1 : ''
    filter == 'sale' ? cond.is_sale = 1 : ''
  };
  if (saleType != 10 && saleType) cond.sale_type = saleType;
  let r = await getList(cond, sort, current, pagesize);
  if (r.status === 0) return res.send({ code: 0, message: '获取失败' });

  // 统计总数据条数
  let total = await count(cond);
  let totalPage = Math.ceil(total / pagesize);

  // totalPage
  res.send({ code: 1, message: '获取成功', list: r, totalPage });
}

let detailCofn = async (req, res) => {
  let { id } = req.query;
  let r = await findOne(id);
  if (r.status === 0) return res.send({ code: 0, message: '获取详情失败' })
  res.send({ code: 1, message: '获取详情成功', info: r[0] });
}

let catesCofn = async (req, res) => {
  let r = await findAll();

  let s = r.reduce((prev, item) => prev.add(item.category), new Set())
  res.send({ code: 1, message: '获取成功', cates: [...s] })
}

let upCofn = async (req, res) => {
  let { id, price, current_price, sale_type, goods_number, is_hot, is_sale } = req.body;
  is_sale = sale_type == 10 ? 0 : 1
  let data = { price, current_price, sale_type, goods_number, is_hot, is_sale };

  let r = await updateOne(id, data);

  if (r.status == 0) return res.send({ code: 0, message: '修改失败,请重试', info: r.message });
  res.send({ code: 1, message: '修改成功' });
}


let reCofn = async (req, res) => {
  let { id } = req.body;

  let r = await delOne(id);

  if (r.status == 0) return res.send({ code: 0, message: '删除失败,请重试', info: r.message });
  res.send({ code: 1, message: '删除成功' });
}

// let addCofn = async (req, res) => {
//   let { title, img_big_logo, img_small_logo, price, sale_type, goods_number, is_hot, category, introduce } = req.body;
//   is_sale = sale_type == '100%' ? 0 : 1
//   let data = {
//     title, img_big_logo, img_small_logo, price, sale_type, goods_number, is_hot, category, introduce
//   }
//   let r = await addOne(data)
// }


let addCofn = async (req, res) => {
let { title, img_big_logo, img_small_logo, price, sale_type, goods_number, is_hot, category, introduce } = req.body;
let current_price = (((sale_type.slice(0, -1) - 0) / 100) * price).toFixed(2) - 0;
let is_sale = sale_type === '100%' ? 0 : 1;
sale_type = sale_type.slice(0, -1) / 10;

// 获取所有商用于统计
let r = await findAll({});

let s1 = r.reduce((prev, item) => prev.add(item.goods_id), new Set());
let goods_id = Math.max(...s1) + 1; // 获取原最大的goods_id 加1 作为 新增的goods_id

let s2 = r.reduce((prev, item) => prev.add(item.category), new Set())
let cateArr = [...s2] // 获取所有类别
// 类别必须是 原有类别中的一种
if (!cateArr.includes(category)) return res.send({ code: 0, message: '添加失败,category参数必须是分类数据表中的分类' });

let r2 = await insertMany({ title, img_big_logo, img_small_logo, price, sale_type, goods_number, is_hot, category, goods_id, is_sale, current_price, goods_introduce: introduce })
console.log;

if (r2.status == 0) return res.send({ code: 0, message: '添加失败,请重试', info: r2.message });

res.send({ code: 1, message: '添加成功' });
}



// 导出
module.exports = {
  insertManyCofn, listCofn, detailCofn, catesCofn, upCofn, reCofn, addCofn
}